<?php

namespace App\Http\Controllers\Admin;

use App\Http\Requests\ArticleRequest;
use App\Models\Admin;
use App\Models\Article;
use App\Models\ArticleCount;
use App\Models\Cate;
use Illuminate\Http\Request;

class ArticleController extends BaseController
{
    //文章列表
    public function index(Request $request,Article $article)
    {
        //判断是否为ajax请求
        if($request->ajax()){
            return $article->getList($request);
        }
        return view('admin.article.index',compact('article'));
    }
    //文章访问统计
    public function count(Request $request){
        $art_id=$request->get('art_id');
        //分组统计
        $data=ArticleCount::select(\DB::raw('sum(click) as count,rid'))->with('renting')->where('art_id',$art_id)->groupBy('rid')->get();
        return view('admin.article.count',compact('data'));
    }
   //添加文章显示
    public function create()
    {
        //读取所有分类，分层级显示
        $cateData=Cate::get()->toArray();
        $cateData=treeLevel($cateData);
        return view('admin.article.create',compact('cateData'));
    }

    //文章添加显示
    public function store(ArticleRequest $request)
    {
        //接收数据入库
        $data=$request->except(['_token','file']);
        Article::create($data);
        return redirect(route('admin.article.index'));
    }
    public function show(Article $article)
    {
        //
    }
    //修改显示
    public function edit(Article $article)
    {
        //读取所有分类，分层级显示
        $cateData=Cate::get()->toArray();
        $cateData=treeLevel($cateData);
        return view('admin.article.edit',compact('cateData','article'));
    }
    //修改数据处理
    public function update(ArticleRequest $request, Article $article)
    {
        $article->update($request->except(['_token','_method','file']));
        return redirect(route('admin.article.index'))->with('success','修改成功');
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  \App\Models\Article  $article
     * @return \Illuminate\Http\Response
     */
    public function destroy(Article $article)
    {
        $article->delete();
        return ['code'=>200,'msg'=>'删除成功'];
    }
}
